<?php
class model_User extends Model
{
    protected $sql = array(
        'login' => 'SELECT id, login FROM users WHERE login = :login AND password = :password'
    );
    
    function login($params)
    {
        $result = $this->execute('login', array('login' => $params['login'], 'password' => sha1($params['password'])));
        if (isset($result[0]))
        {
            self::init();
            $_SESSION['user'] = $result[0];
        }
        return $result;
    }
    
    function logout()
    {
        self::init();
        session_destroy();
        unset($_SESSION['user']);
    }
    
    function getCurrentUser()
    {
        return new Result($this, 'getCurrentUserData');
    }
    
    function getCurrentUserData()
    {
        self::init();
        return new Result(empty($_SESSION['user']) ? array() : array($_SESSION['user']));
    }
    
    private static function init()
    {
        if (!session_id()) session_start();
    }
}
?>